<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Articles extends OC_Controller {

	function __construct()
    {
        parent::__construct();
        $this->load->model('articles/article_model','am');
        $this->load->model('home/home_model');
        $this->load->library('recaptcha');
        $this->load->model('events/events_model');
    }

    function _remap($method, $params = array())
    {
        parent::_remap($method, $params);

        if (method_exists($this, $method))
            return call_user_func_array(array($this, $method), $params);
        else
            show_404();
    }

    function init_header()
    {
        $this->header['meta'] = array(
            'description' => array('name' => 'description', 'content' => 'Haan Kreasi Ibu Juara'),
            'keywords' => array('name' => 'keywords', 'content' => 'Haan Kreasi Ibu Juara, recipes, kreasi ibu, kreatif, sajian  kreatif'),
        );
    }

    public function index()
    {
    	$count = $this->am->count(0,0,false);

    	$this->load->library('Pagination');
        $config['total_rows'] = $count;
        $config['per_page'] = 2;
        $config['base_url'] = base_url().index_page().'/articles/index/';

        /*style pagination*/
		$config['first_link	']			= '&lsaquo; First';
		$config['next_link']			= '&gt;&gt;';
		$config['prev_link']			= '&lt;&lt;';
		$config['last_link']			= 'Last &rsaquo;';
		//$config['uri_segment']			= 3;
		$config['full_tag_open']		= '<li>';
		$config['full_tag_close']		= '</li>';
		$config['first_tag_open']		= '<li>';
		$config['first_tag_close']		= '</li>';
		$config['last_tag_open']		= '<li>';
		$config['last_tag_close']		= '</li>';
		$config['cur_tag_open']			= '<li class="active"><span>';
		$config['cur_tag_close']		= '</span></li>';
		$config['next_tag_open']		= '<li>';
		$config['next_tag_close']		= '</li>';
		$config['prev_tag_open']		= '<li>';
		$config['prev_tag_close']		= '</li>';
		$config['num_tag_open']			= '<li>';
		$config['num_tag_close']		= '</li>';

        $this->pagination->initialize($config);
        $this->header['pagination'] = $this->pagination->create_links();

        $offset=$this->uri->segment(3);
        if(empty($offset)){$offset=0;}

        $this->header['items'] = $this->am->get_items($config['per_page'], $offset,false);

        $this->header['widget_home']    = $this->home_model->get_widget();
        $this->header['widget_footer']  = $this->home_model->get_widget(2);
        $this->header['categories']     = $this->am->get_categories();
        $this->header['socmed_home']    = $this->home_model->get_socmed();
        $this->header['get_cat_produk'] = $this->home_model->get_category('category_product','cp_id');
        $this->header['get_cat_resep']  = $this->home_model->get_category('category_recipes','cr_id');
        $this->header['get_cat_article']= $this->home_model->get_category('category_article','ca_id');
        $this->header['recent_articles']    = $this->events_model->get_recent_articles();
        $this->header['recent_products']    = $this->events_model->get_recent_products();
        $this->header['event_home']     = $this->home_model->get_event();
        $this->view('articles/list_article_view');
    }

    public function cat()
    {
        $count = $this->am->count(0,0,$this->uri->segment(3));

        $this->load->library('Pagination');
        $config['total_rows'] = $count;
        $config['per_page'] = 2;
        $config['base_url'] = base_url().index_page().'/articles/index/cat/'.$this->uri->segment(3).'/';

        /*style pagination*/
        $config['first_link ']          = '&lsaquo; First';
        $config['next_link']            = '&gt;&gt;';
        $config['prev_link']            = '&lt;&lt;';
        $config['last_link']            = 'Last &rsaquo;';
        //$config['uri_segment']            = 3;
        $config['full_tag_open']        = '<li>';
        $config['full_tag_close']       = '</li>';
        $config['first_tag_open']       = '<li>';
        $config['first_tag_close']      = '</li>';
        $config['last_tag_open']        = '<li>';
        $config['last_tag_close']       = '</li>';
        $config['cur_tag_open']         = '<li class="active"><span>';
        $config['cur_tag_close']        = '</span></li>';
        $config['next_tag_open']        = '<li>';
        $config['next_tag_close']       = '</li>';
        $config['prev_tag_open']        = '<li>';
        $config['prev_tag_close']       = '</li>';
        $config['num_tag_open']         = '<li>';
        $config['num_tag_close']        = '</li>';

        $this->pagination->initialize($config);
        $this->header['pagination'] = $this->pagination->create_links();

        $offset=$this->uri->segment(4);
        if(empty($offset)){$offset=0;}

        $this->header['items'] = $this->am->get_items($config['per_page'], $offset,$this->uri->segment(3));

        $this->header['widget_home']    = $this->home_model->get_widget();
        $this->header['widget_footer']  = $this->home_model->get_widget(2);
        $this->header['categories']     = $this->am->get_categories();
        $this->header['socmed_home']    = $this->home_model->get_socmed();
        $this->header['get_cat_produk'] = $this->home_model->get_category('category_product','cp_id');
        $this->header['get_cat_resep']  = $this->home_model->get_category('category_recipes','cr_id');
        $this->header['get_cat_article']= $this->home_model->get_category('category_article','ca_id');
        $this->header['recent_articles']    = $this->events_model->get_recent_articles();
        $this->header['recent_products']    = $this->events_model->get_recent_products();
        $this->header['event_home']     = $this->home_model->get_event();
        $this->view('articles/list_article_view');
    }


    public function detail($title=false,$id=false)
    {

        $this->header['meta'] = array(
            'description' => array('name' => 'description', 'content' => $this->am->get_meta('descriptions',$id)),
            'keywords' => array('name' => 'keywords', 'content' => $this->am->get_meta('keywords',$id)),
        );


        $this->header['detail'] = $this->am->get_item_details($id);

        $this->header['widget_home']    = $this->home_model->get_widget();
        $this->header['categories']    = $this->am->get_categories();
        $this->header['comments']    = $this->am->get_comments($id);
        $this->header['recaptcha_html'] = $this->recaptcha->recaptcha_get_html();
        $this->header['socmed_home']    = $this->home_model->get_socmed();
        $this->header['get_cat_produk'] = $this->home_model->get_category('category_product','cp_id');
        $this->header['get_cat_resep']  = $this->home_model->get_category('category_recipes','cr_id');
        $this->header['get_cat_article']= $this->home_model->get_category('category_article','ca_id');
        $this->header['recent_articles']    = $this->events_model->get_recent_articles();
        $this->header['recent_products']    = $this->events_model->get_recent_products();
        $this->header['event_home']     = $this->home_model->get_event();
        $this->view('articles/detail_article_view');
    }

    public function do_comment()
    {
    	$this->recaptcha->recaptcha_check_answer();
        $title = $this->input->post('a_title');
        if($this->input->post('submit'))
        {
            unset($_POST['recaptcha_challenge_field']);
            unset($_POST['recaptcha_response_field']);
            unset($_POST['submit']);
            unset($_POST['a_title']);

            if($this->recaptcha->getIsValid()){
                $_POST['ca_created_date'] = date('Y-m-d H:i:s');
                $do_add = $this->am->do_comment($_POST);
                if($do_add){
                    redirect('articles/detail/'.slugify($title).'/'.$this->input->post('a_id'), 'refresh');
                }else{
                    redirect('articles/detail/'.slugify($title).'/'.$this->input->post('a_id'), 'refresh');
                }
            }else{
                redirect('articles/detail/'.slugify($title).'/'.$this->input->post('a_id'), 'refresh');
            }
        }
    }

}

/* End of file articles.php */
/* Location: ./applications/module_front/articles/controllers/articles.php */